package pers.ljf.escloud.filters;

import org.springframework.stereotype.Component;
import pers.ljf.escloud.common.dto.AppLoginUserInfo;
import pers.ljf.escloud.common.utils.UserInfoUtils;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

@WebListener
@Component
public class SessionAttributeListener implements HttpSessionAttributeListener {
    @Override
    //创建session时触发
    public void attributeAdded(HttpSessionBindingEvent event) {
        if ("userInfo".equals(event.getName())) {
            UserInfoUtils.set((AppLoginUserInfo) event.getValue());
        }
    }

    @Override
    //销毁session时触发
    public void attributeRemoved(HttpSessionBindingEvent event) {
        if ("userInfo".equals(event.getName())) {
            UserInfoUtils.remove();
        }
    }

    @Override
    //替换session时触发
    public void attributeReplaced(HttpSessionBindingEvent event) {
        if ("userInfo".equals(event.getName())) {
            UserInfoUtils.set((AppLoginUserInfo) event.getValue());
        }
    }
}
